<!--
 * @Author: 黑白灰01 3193273703@qq.com
 * @Date: 2025-01-27 10:57:57
 * @LastEditors: 黑白灰01 3193273703@qq.com
 * @LastEditTime: 2025-02-12 12:39:45
 * @FilePath: /hei_picture_web/src/page/home/HomeIndex.vue
 * @Description: 首页
-->
<template>
  <div id="homeIndexPage">
    <!-- 搜索框 -->
    <div class="search-bar">
      <a-input-search
        v-model:value="searchParams.searchText"
        placeholder="从海量图片中搜索"
        enter-button="搜索"
        allow-clear
        size="large"
        @search="doSearch"
      />
    </div>
    <!-- 分类和标签筛选 -->
    <a-tabs v-model:active-key="selectedCategory" @change="doSearch">
      <a-tab-pane key="all" tab="全部" />
      <a-tab-pane v-for="category in categoryList" :tab="category" :key="category" />
    </a-tabs>
    <div class="tag-bar">
      <span style="margin-right: 8px">标签：</span>
      <a-space :size="[0, 8]" wrap>
        <a-checkable-tag
          v-for="(tag, index) in tagList"
          :key="tag"
          v-model:checked="selectedTagList[index]"
          @change="doSearch"
        >
          {{ tag }}
        </a-checkable-tag>
      </a-space>
    </div>
    <PictureList :dataList="pictureList" :loading="loading" />
    <!-- 分页 -->
    <a-pagination
      class="pagination"
      v-model:current="searchParams.current"
      v-model:page-size="searchParams.pageSize"
      :page-size-options="pageSizeOptions"
      :total="total"
      :show-total="(total: number) => `共 ${total} 张图片`"
      show-size-changer
      @showSizeChange="onShowSizeChange"
    />
  </div>
</template>

<script setup lang="ts">
import {
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost,
} from '@/api/pictureController'
import { onMounted, reactive, ref } from 'vue'
import PictureList from '@/page/home/modal/PictureList.vue'
import { message } from 'ant-design-vue'

// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({
  current: 1,
  pageSize: 12,
  sortField: 'createTime',
  sortOrder: 'descend',
})

// 标签和分类列表
const categoryList = ref<string[]>([])
const selectedCategory = ref<string>('all')
const tagList = ref<string[]>([])
const selectedTagList = ref<boolean[]>([])

const pictureList = ref<API.PictureVO[]>([])
const loading = ref<boolean>(false)
const total = ref<number>(0)
const fetchData = async () => {
  loading.value = true
  // 转换搜索参数
  const params = {
    ...searchParams,
    tags: [] as string[],
  }
  if (selectedCategory.value !== 'all') {
    params.category = selectedCategory.value
  }
  // [true, false, false] => ['java']
  selectedTagList.value.forEach((useTag, index) => {
    if (useTag) {
      params.tags.push(tagList.value[index])
    }
  })
  // console.log(params)

  const res = await listPictureVoByPageUsingPost(params)
  pictureList.value = res.data?.data?.records ?? []
  total.value = res.data?.data?.total ?? 0
  loading.value = false
}
const pageSizeOptions = ['12', '24']
const onShowSizeChange = (page: number, pageSize: number) => {
  searchParams.current = page
  searchParams.pageSize = pageSize
  fetchData()
}
/**
 * 获取标签和分类选项
 * @param values
 */
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 200 && res.data.data) {
    tagList.value = res.data.data.tagList ?? []
    categoryList.value = res.data.data.categoryList ?? []
  } else {
    message.error('获取标签分类列表失败，' + res.data.message)
  }
}
// 搜索
const doSearch = () => {
  // 重置搜索条件
  searchParams.current = 1
  fetchData()
}

onMounted(() => {
  getTagCategoryOptions()
  fetchData()
})
</script>

<style scoped lang="scss">
#homeIndexPage {
  .search-bar {
    max-width: 480px;
    margin: 0 auto 16px;
  }
  .tag-bar {
    margin-bottom: 16px;
  }
  .pagination {
    margin-top: 16px;
    margin-bottom: 16px;
    float: right;
  }
}
</style>
